<!DOCTYPE html>

<html lang="en">

<head>
<meta charset="UTF-8">
<title>FORM Text</title>
<link rel="stylesheet" id="templatecss" type="text/css" href="style.css">
<script type="text/javascript" src="jquery.min.js"></script>
</head>
<body>
    <form action="" method="post" class="basic-grey left">
        <h1>
            填写表单 <span>请将要发布的信息填写完整</span>
        </h1>
        <label>                           
			<span>通知标题:</span> 
			<input id="noticeName" type="text" name="name" placeholder="Notice Name" />          
		</label>
        <label> 
			<span>发	布者单位 :</span>                           
			<input id="maker" type="text" name="name" placeholder="Team Name" />
		</label>
        <label>                           
			<span>通知内容 :</span>
			<textarea id="message" name="message" placeholder="The Message Content"></textarea>                 
		</label>
        <label>                           
			<span>截止日期 :</span>
			<input type="datetime-local" id="dataChoose" />
		</label>
        <label>                          
			<div id="subject">
				<span>发布对象 :</span> <input type="checkbox" name="category"
					value="所有人" checked="checked" />所有人 <input type="checkbox"
					name="category" value="班长" />班长 <input type="checkbox"
					name="category" value="体育委员" />体育委员 <input type="checkbox"
					name="category" value="学习委员" />学习委员 <input type="checkbox"
					name="category" value="团支书" />团支书 <input type="checkbox"
					name="category" value="党员" />党员
			</div>
		</label>

        <label>                           
			<span>&nbsp;</span>
			<input type="button" class="button" value="提交" id="submit" />                
		</label>
    </form>

    <form id="fieldForm" class="basic-grey right">
        <h1>
            填写表单 <span>请选择学生需要填写的信息</span>
        </h1>
        <label>                           
            <span>填写字段1 :</span>
            <input type="text" id="field1" />
        </label>
        <label>                           
            <span>&nbsp;</span>
            <input type="button" class="button" value="添加新字段" id="addField" />                
        </label>

    </form>
</body>
	<script>
		$(function() {
			 var cnt = 1;
	         $('#addField').on("click", function() {
	            var num = "#field" + cnt++;
	            $(num).after("<label> <span>填写字段" + cnt + " :</span>  <input type='text' id='field" + cnt + "' />  </label>")
	            
	         })
			$('#submit').click(function() {
				var noticeName = $('#noticeName').val();
				var maker = $('#maker').val();
				var message = $('#message').val();
				var dataChoose = $('#dataChoose').val();
				dataChoose = dataChoose.split('T')[0];
				var chk_value = [];
				$('input[name="category"]:checked').each(function() {
					chk_value.push($(this).val());
				});
				noticeName = encodeURI(noticeName);
				maker = encodeURI(maker);
				message = encodeURI(message);
				dataChoose = encodeURI(dataChoose);
				chk_value = encodeURI(chk_value);
				var field_value = [];
	            for (var i = 1; i <=cnt; i++) {
	                var num = "#field" + i;
	                if($(num).val()==""){
	                	alert("字段不能为空!")
	                	return ;
	                }
	                field_value.push($(num).val())
	            }
	            field_value = encodeURI(field_value);
	            
				$.ajax({
					url : "/MyWebPlus/insertNoticeInformation",
					data : {
						"noticeName" : noticeName,
						"maker" : maker,
						"message" : message,
						"dataChoose" : dataChoose,
						"chk_value" : chk_value
					},
					async : false,
					dataType : "text",
					success : function(data) {
						 $.ajax({
								url : "/MyWebPlus/insertNoticeInformation",
								data : {"field_value" : field_value},
								dataType : "text",
								async : false,
								success : function(data) {
									if (data >= 0) {
										alert("发布成功 !");
									} else {
										alert("发布失败!");
									}
								}
							});
					},
					failure:function(){
						alert("发布失败,请检查内容!");
						return ;
					}
				});
				
	            
	           
			});
		});
	</script>

</html>